﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Admin_ProductManager : System.Web.UI.Page
{
    SanPhamCtr SanPhamCtr = new SanPhamCtr();
    TimKiemCtr timKiemCtr = new TimKiemCtr();
    void laySanPham()
    {
        
        gvSanPham.DataSource = SanPhamCtr.GetSanPhamH();
        gvSanPham.DataBind();
        gvSanPham.DataKeyNames = new string[] { "MaSP"};
       
    }
    void Lien_ket_DL(string pCot)//Sap xep
    {
        
        if (pCot == "")
        {

            gvSanPham.DataSource = SanPhamCtr.GetSanPhamH();
        }
        else
        {
            gvSanPham.DataSource = SanPhamCtr.GetSanPham1H(pCot);
        }




        gvSanPham.DataBind();
        gvSanPham.DataKeyNames = new string[] { "MaSP" };
        int tongSanPham = SanPhamCtr.countProduct();
        lbproducts.Text = tongSanPham.ToString();

    }
    public void checkAlreadyLogin()
    {
        if ((Session["UserType"].ToString() != "QuanTri" && Session["UserType"].ToString() != "NhanVien") || Session["UserID"].ToString() == null||Session["QuanTri"].ToString()==null)
            Response.Redirect("Login.aspx");
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            checkAlreadyLogin();
            if (Request.QueryString["ChuoiTimKiem"] == "")
            {
                //laySanPham();
                int tongSanPham = SanPhamCtr.countProduct();
                lbproducts.Text = tongSanPham.ToString();
            }
            else
            {
                string tenSP = Request.QueryString["ChuoiTimKiem"];
                DataTable tksp=timKiemCtr.TimKiemSPTheoTen(tenSP);
                if (tksp.Rows.Count > 0)
                {
                    gvSanPham.DataSource = tksp;
                    gvSanPham.DataBind();
                    gvSanPham.DataKeyNames = new string[] { "MaSP" };
                }
                else
                {
                    lbsp.Text = "Không có sản phẩm nào";
                }
                int tongSanPham = SanPhamCtr.countProduct();
                lbproducts.Text = tongSanPham.ToString();
            }
           
        }

    }
    protected void gvSanPham_Sorting(object sender, GridViewSortEventArgs e)
    {
        Lien_ket_DL(e.SortExpression);
    }
    protected void gvSanPham_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {


            if (e.CommandName.Equals("Edit"))
            {
                string maSP = e.CommandArgument.ToString();

                Response.Redirect("EditProduct.aspx?id=" + maSP);
            }
        }
        catch
        {
        }
    }

    protected void gvSanPham_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string MaSP = gvSanPham.DataKeys[e.RowIndex].Value.ToString();

        SanPhamCtr SanPhamCtr = new SanPhamCtr();
        bool kq = SanPhamCtr.DeleteSanPhamH(MaSP);
        if (kq)
        {

            Response.Write("<script language=\"javascript\" type\"text/javascript\">alert('Xóa sản phẩm thành công');window.location=\"ProductManager.aspx?ChuoiTimKiem=\"</script>");
        }
        else
        {
            Response.Write("<script language=\"javascript\" type\"text/javascript\">alert('Xóa sản phẩm thất bại!');window.location=\"ProductManager.aspx?ChuoiTimKiem=\"</script>");
        }
        laySanPham();
    }

    protected void gvSanPham_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvSanPham.PageIndex = e.NewPageIndex;
        laySanPham();
    }
    protected void bttimkiem_Click(object sender, EventArgs e)
    {
        if (String.IsNullOrEmpty(txttimkiem.Text))
        {
            laySanPham();
        }
        else
        {
            //TimKiemCtr timKiemCtr = new TimKiemCtr();
            //gvSanPham.DataSource = timKiemCtr.TimKiemSPTheoTen(txttimkiem.Text);
            //gvSanPham.DataBind();
            //int tongSanPham = SanPhamCtr.countProduct();
            //lbproducts.Text = tongSanPham.ToString();
            Response.Redirect("SearchResult.aspx?LoaiTK=1&ChuoiTimKiem=" + txttimkiem.Text);
        }
    }
    protected void gvSanPham_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton btn = (LinkButton)e.Row.Cells[4].Controls[1];
            //khi click vao nut xoa se phat sinh doan ma javascript thong bao xac nhan co muon xoa hay khong
            btn.OnClientClick = "return confirm('Bạn có chắc chắn xóa sản phẩm : " + Server.HtmlDecode(e.Row.Cells[1].Text) + " này không?')";
        }
    }
   
}